﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SN.Services;
using WeifenLuo.WinFormsUI.Docking;
using SN.Models;
using SN.Data;
namespace SN.Report
{
    public partial class FormLeft : SN.Report.FormBase
    {
        private Point pi;

        private MainForm mdi;

        public MainForm Mdi
        {
            get { return mdi; }
            set { mdi = value; }
        }
        private static FormLeft instance = null;
        //添加一个属性
        public static FormLeft Instance
        {
            set
            {
            }
            get
            {
                if (instance == null)
                {
                    new FormLeft();
                }
                return instance;
            }
        }

        public FormLeft()
        {
            InitializeComponent();
            // instance = this;
        }

        private void FormLeft_Load(object sender, EventArgs e)
        {
            LoadTree();
        }

        private void LoadTree()
        {
            SNService.Hourglass(true);
            try
            {
                treeView1.Nodes.Clear();

                List<T_Classes> ClassesList = (List<T_Classes>)DBUtility.DB.From<T_Classes>().Where(WhereClip.All).ToList();
                foreach (T_Classes item in ClassesList)
                {
                    TreeNode Pnode = new TreeNode() { Text = item.Classes_Name+"-"+item.Classes_ID, Tag = item.Classes_ID };
                    Pnode.SelectedImageIndex = 0;
                    Pnode.ImageIndex = 0;
                    treeView1.Nodes.Add(Pnode);

                    List<T_Report> ReportList = (List<T_Report>)DBUtility.DB.From<T_Report>().Select(T_Report._.Report_ID, T_Report._.Report_Name).Where(T_Report._.Report_ClassesID == item.Classes_ID).ToList();
                    foreach (T_Report Report in ReportList)
                    {
                        TreeNode node2 = new TreeNode() { Text = Report.Report_Name+"-"+Report.Report_ID, Tag = Report.Report_ID };
                        node2.ImageIndex = 1;
                        node2.SelectedImageIndex = 1;
                        Pnode.Nodes.Add(node2);
                    }
                }
            }
            catch (Exception err)
            {
                SNService.Hourglass(false);
                SNService.ShowError(err.Message);
            }
            finally
            {
                SNService.Hourglass(false);
            }
        }

        private void FormLeft_FormClosed(object sender, FormClosedEventArgs e)
        {
            return;
        }

        //private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        //{
        // dbTreeNode node; = (dbTreeNode)e.Node;
        //if (node != null)
        //{
        //    switch (node.GroupName.ToUpper())
        //    {
        //        case "T_REPORT":
        //            FormTemp frm = new FormTemp();
        //            frm.Text = node.Text;
        //            //类名
        //            SN.Models.T_Report report = SN.Data.DBUtility.DB.From<SN.Models.T_Report>().Where(SN.Models.T_Report._.Report_ID == node.Value.ToString()).ToSingle();

        //            frm.Report = report;
        //            if (mdi.CheckChildOpenState(mdi, frm) == 0) //判断是否已经打开
        //            {
        //                frm.Show(this.Mdi.dockPanel);
        //                frm.ReadBinaryData(node.Value.ToString()); //读取报表
        //            }
        //            else
        //            {
        //                frm.BringToFront();
        //            }
        //            break;
        //        case "T_CLASSES":
        //            break;
        //    }

        //}
        //}

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            LoadTree();
        }

        //private void TreeView1_DoubleClick(object sender, EventArgs e)
        //{

        //    //关键步骤：
        //    //1、根据鼠标位置得到当前活动节点。当然这一步得到的仍然可能在意料之外，因为TreeNode右边的空白位置算作它的区域。       
        //    //2、判断鼠标的位置是否真的在节点上。可以引入一个成员变量来记录鼠标按下去或者弹起来的位置；也可以动态的抓取鼠标当前的绝对位置，
        //    //然后使用TreeView控件的PointToClient方法换算成空间内部的相对位置，来看是否在节点范围内部。
        //    dbTreeNode node = (dbTreeNode)this.TreeView1.GetNodeAt(pi);
        //    if (pi.X < node.Bounds.Left || pi.X > node.Bounds.Right)
        //    {
        //        //不触发事件
        //    }
        //    else
        //    {
        //        //触发事件
        //        if (node != null)
        //        {
        //            switch (node.GroupName.ToUpper())
        //            {
        //                case "T_REPORT":
        //                    DockContent frm = FindDocument(node.Text);
        //                    SN.Models.T_Report report = SN.Data.DBUtility.DB.From<SN.Models.T_Report>().Where(SN.Models.T_Report._.Report_ID == node.Value.ToString()).ToSingle();
        //                    if (frm == null)
        //                    {
        //                        FormTemp ft = new FormTemp();
        //                        ft.Report = report;
        //                        ft.Text = node.Text;
        //                        ft.Show(this.Mdi.dockPanel);
        //                        ft.ReadBinaryData(node.Value.ToString()); //读取报表
        //                    }
        //                    else
        //                    {
        //                        frm.Activate();
        //                        frm.BringToFront(); //放到最前方
        //                        frm.Focus();
        //                    }
        //                    break;
        //                case "T_CLASSES":
        //                    break;
        //            }
        //        }
        //    }
        //}



        #region 根据名称查找 是否该文档已经打开
        private DockContent FindDocument(string text)
        {
            if (this.mdi.dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    if (form.Text == text)
                    {
                        return form as DockContent;
                    }
                }
                return null;
            }
            else
            {
                foreach (DockContent content in this.mdi.dockPanel.Documents)
                {
                    if (content.DockHandler.TabText == text)
                    {
                        return content;
                    }
                }
                return null;
            }
        }
        #endregion

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
           
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            if (e == null) return;
            switch (treeView1.SelectedNode.Level)
            {
                case 1:
                    DockContent frm = FindDocument(treeView1.SelectedNode.Text);
                    SN.Models.T_Report report = SN.Data.DBUtility.DB.From<SN.Models.T_Report>().Where(SN.Models.T_Report._.Report_ID == treeView1.SelectedNode.Tag.ToString()).ToSingle();
                    if (frm == null)
                    {
                        FormTemp ft = new FormTemp();
                        ft.Report = report;
                        ft.Text = treeView1.SelectedNode.Text;
                        ft.Show(this.Mdi.dockPanel);
                        ft.ReadBinaryData(treeView1.SelectedNode.Tag.ToString()); //读取报表
                    }
                    else
                    {
                        frm.Activate();
                        frm.BringToFront(); //放到最前方
                        frm.Focus();
                    }
                    break;
                default:
                    break;
            }
            return;

        }
    }
}
